#include <DApplication>
#include <QVector>
#include <QPainter>
#include <QMouseEvent>
#include <QMenu>
#include "FloatTaskItem.h"
#include "../DataCenter.h"

DWIDGET_USE_NAMESPACE

void FloatTaskItem::initFloatTaskItems()
{
    QVector<FloatedData> tasks;
    DataCenter::instance().getFloatTask(tasks);
    for (auto &task : tasks)
    {
        FloatTaskItem *item = new FloatTaskItem;
        item->setTask(task.title, true);
        item->move(task.location);
        item->show();
    }
}

FloatTaskItem::FloatTaskItem() : QWidget(nullptr), checkBox(this)
{
    setWindowFlags(Qt::WindowStaysOnBottomHint | Qt::Tool);
    setAttribute(Qt::WA_X11NetWmWindowTypeDock);
    setAttribute(Qt::WA_TranslucentBackground);
    setFixedSize(220, 56);

    checkBox.setGeometry(10, 0, 150, 50);

    connect(&checkBox, &QCheckBox::clicked, this, &FloatTaskItem::onChecked);

    connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &FloatTaskItem::onThemeChanged);
    onThemeChanged(DGuiApplicationHelper::instance()->themeType());

    connect(qApp, &QCoreApplication::aboutToQuit, this, &FloatTaskItem::onAppQuit);

}

FloatTaskItem::~FloatTaskItem()
{

}

void FloatTaskItem::setTask(const QString &title, bool isSaved)
{
    checkBox.setText(title);
    if (!isSaved)
        DataCenter::instance().createFloatTask(title);
}

bool FloatTaskItem::isChecked()
{
    return checkBox.isChecked();
}

void FloatTaskItem::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(m_backgroundColor);
    painter.drawRoundedRect(rect(), 8, 8);
}

bool FloatTaskItem::event(QEvent *event)
{
    QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
    if (mouseEvent)
    {
        lower();
        if (mouseEvent->type() == QEvent::MouseButtonPress)
        {
            movePoint = mouseEvent->windowPos().toPoint();
        }
        if (mouseEvent->type() == QEvent::MouseMove)
        {
            move(mouseEvent->globalPos() - movePoint);
        }
        if (mouseEvent->button() == Qt::RightButton)
        {
            QMenu menu;
            QAction removeAction;
            removeAction.setText("删除");
            menu.addAction(&removeAction);
            if (menu.exec(mouseEvent->globalPos()) == &removeAction)
            {
                DataCenter::instance().removeFloatTask(checkBox.text());
                this->deleteLater();
            }
        }
        return true;
    }
    else
    {
        return QWidget::event(event);
    }
}


void FloatTaskItem::onThemeChanged(DGuiApplicationHelper::ColorType type)
{
    if (type == DGuiApplicationHelper::ColorType::DarkType)
    {
        m_backgroundColor = QColor(0, 0, 0, 180);
    }
    else
    {
        m_backgroundColor = QColor(255, 255,255, 180);
    }
}

void FloatTaskItem::onChecked(bool state)
{

}

void FloatTaskItem::onAppQuit()
{
    if (isChecked())
        DataCenter::instance().removeFloatTask(checkBox.text());
    else
        DataCenter::instance().setFloatTask(checkBox.text(), pos());
}
